﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SaaS.Core;
using SaaS.Model.Enums;
using Web.System.Services.Blogs;
using SaaS.Model.Dto.Blogs;
using SaaS.Model.Dto.Auths;
using XCore.Authorization;
using XCore.Domain.Dto;
using XCore.Domain.Dto.Page;
using XCore.Extensions;
using SaaS.Model.Entity.Blogs;

namespace Web.System.Controllers.Blogs
{
    [Route("api/blog/[controller]/[action]")]
    [ApiController]
    public class ArticleController : SystemControllerBase
    {
        private readonly ArticleAppService _articleApplicationSerivce;
        public ArticleController(ArticleAppService articleApplicationService)
        {
            _articleApplicationSerivce = articleApplicationService;
        }
        [HttpPost]
        public ActionResult List(PagedInput input)
        {
            var query = SDC.Articles.AsQueryable();
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Id,
                c.Flow,
                c.Sort,
                c.Tags,
                c.Title,
                c.Status,
                c.CreationTime,
            });
        }
        /// <summary>
        /// 文章留言
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Messages(PagedByIdInput input)
        {
            var query = SDC.ArticleMessages.Where(c => c.ArticleId == input.Id);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.ParentId,
                c.Content,
                c.CreationTime,
                c.CreatorUserId
            });
        }
        /// <summary>
        /// 查询版本列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Versions(PagedByIdInput input)
        {
            var query = SDC.ArticleVersions.Where(c => c.Id == input.Id);
            return PageResult(query.OrderByDescending(c => c.CreationTime), c => new
            {
                c.Id,
                c.CreationTime
            });
        }
        [HttpGet]
        public ActionResult Version(int id)
        {
            var entity = SDC.ArticleVersions.FirstOrDefault(c => c.Id == id);
            return Ok(entity);
        }
        /// <summary>
        /// 回复
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Reply(ArticleMessageInput input)
        {
            return Ok(_articleApplicationSerivce.Reply(input));
        }
        [HttpPost]
        public ActionResult Save(ArticleInput input)
        {
            return Ok(_articleApplicationSerivce.Save(input));
        }
        [HttpGet]
        public ActionResult Info(int id)
        {
            return Ok(SDC.Articles.Where(c => c.Id == id).Select(c => new
            {
                c.Tags,
                c.Id,
                c.Title,
                c.Status,
                c.Content
            }).FirstOrDefault());
        }
        [HttpDelete]
        public ActionResult Delete(int id)
        {
            return Ok(_articleApplicationSerivce.Delete(id));
        }
        [HttpPost]
        public ActionResult Upload()
        {
            string jsonString = HttpContext.Upload(CoreSetting.UtilServer.Url + UploadType.Blog.GetDescription());
            if (jsonString.IsNullOrWhiteSpace()) return Ok("无上传文件");
            return Ok(CoreSetting.UtilServer.Url + jsonString.ToObject<Result>().Data);
        }
        [HttpGet]
        public ActionResult Tags()
        {
            return Ok(SDC.ArticleTags.Select(c => c.Name));
        }
        [HttpPost]
        public ActionResult SaveTag(string name)
        {
            return Ok(_articleApplicationSerivce.SaveTag(name));
        }
        [HttpDelete]
        public ActionResult DeleteTag(string name)
        {
            return Ok(_articleApplicationSerivce.DeleteTag(name));
        }
    }
}